package com.example.finaltask;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentTransaction;

import android.content.Intent;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.example.finaltask.Fragment.InfoFragment;
import com.example.finaltask.Fragment.PostFragment;
import com.example.finaltask.Service.MusicService;
import com.example.finaltask.modle.User;
import com.example.finaltask.utils.Remove;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.io.Serializable;

public class IndexActivity extends AppCompatActivity {
    private Fragment mCurrentFragment = null;
    private Fragment[] fragments;
    private ImageView headImage;
    private User user;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        Remove.removeTop(this);
        baseInfo();
        //判断音乐播放的状态
        headImage.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    //跳转信息页面,传递对象
                    Intent intent = new Intent();
                    intent.setClass(IndexActivity.this, ExitActivity.class);
                    Bundle bundle = new Bundle();
                    bundle.putSerializable("user", user);
                    intent.putExtras(bundle);
                    startActivity(intent);
                }
            });
    }

    /**
     *@description 页面切换
     */
    private void showFragment(int position) {
        Fragment fragment = fragments[position];
        if (mCurrentFragment != fragment) {
            FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
            transaction.hide(mCurrentFragment);
            mCurrentFragment = fragment;
            if (!fragment.isAdded())
                transaction.add(R.id.container, fragment);
            else
                transaction.show(fragment);
            transaction.commitAllowingStateLoss();
        }
    }

    private void baseInfo(){
        PostFragment postFragment = new PostFragment();
        InfoFragment infoFragment = new InfoFragment();
        fragments = new Fragment[]{postFragment, infoFragment};
        BottomNavigationView navigation = findViewById(R.id.nav_view);
        navigation.setOnNavigationItemSelectedListener(
                new BottomNavigationView.OnNavigationItemSelectedListener() {
                    @Override
                    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                        switch (item.getItemId()) {
                            case R.id.navigation_home: showFragment(0);return true;
                            case R.id.navigation_info: showFragment(1);return true;
                        }
                        return false;
                    }
                });
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        //装入第一页
        transaction.add(R.id.container,postFragment).commit();
        mCurrentFragment = postFragment;
        //头像
        headImage = findViewById(R.id.head_image);
        //用户信息
        Intent intent = this.getIntent();
        user = (User) intent.getSerializableExtra("user");

        Bundle bundle = new Bundle();
        bundle.putSerializable("user", (Serializable) user);
        //给fragment传递User对象
        postFragment.setArguments(bundle);;
        if(user != null){
            Glide.with(this).load(user.getHeadImage()).into(headImage);
        }else{
            //默认头像
            headImage.setBackground(getDrawable(R.drawable.default1));
        }
        // 启动服务，从而实现进入应用时就播放背景音乐
         startService(new Intent(IndexActivity.this,MusicService.class));
         super.onStart();
    }
}